import { ElMessage, ElLoading, ElMessageBox } from 'element-plus'

/* 四类弹窗提示 */
export function $success(message = '', duration = 3000) {
  ElMessage({
    message,
    type: 'success',
    duration
  })
}
export function $warn(message = '', duration = 3000) {
  ElMessage({
    message,
    type: 'warning',
    duration
  })
}
export function $error(message = '', duration = 3000) {
  ElMessage({
    message,
    type: 'error',
    duration
  })
}
export function $msg(message = '', duration = 3000) {
  ElMessage({
    message,
    duration
  })
}
/**
 * 确认框
 */
export function $confirm(msg = '', success?:Function, cancel?:Function) {
  ElMessageBox.confirm(
    msg,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      success && success()
    })
    .catch(() => {
      cancel && cancel()
    })
}

/* 加载动画 */
let loading:any

export function $loading(text = '', showBg = true) {
  loading = ElLoading.service({
    lock: true,
    text,
    background: showBg ? 'rgba(255, 255, 255, 0.7)' : 'rgba(0,0,0,0)',
  })
}
export function $hideLoading() {
  loading && loading.close()
}

import router from '@/router'
import type { RouteName } from '@/modules/route'
/**
 * 根据name跳转路由
 * @param {String} name 路由名或路径
 * @param {Object} query 路由参数，加在路径上
 */
export function $pushTo(name:RouteName, query = {}, params = {}) {
  if (!name) {
    return
  }
  if (name.startsWith('/')) {
    router.push({ path: name, query, params })
  } else {
    router.push({ name, query, params })
  }
}
/**
 * 根据name跳转路由
 * @param {String} name 路由名
 * @param {Object} query 路由参数，加在路径上
 */
export function $replaceTo(name:RouteName, query = {}, params = {}) {
  if (!name) {
    return
  }
  if (name.startsWith('/')) {
    router.replace({ path: name, query, params })
  } else {
    router.replace({ name, query, params })
  }
}
